<template>
  <div class="svc-overview">
    <div class="page-header">
      <h1>服务贡献总览</h1>
      <div class="filters">
        <el-select v-model="q.type" placeholder="类型" clearable style="width: 140px;">
          <el-option label="临时性" value="临时性" />
          <el-option label="长期性" value="长期性" />
        </el-select>
        <el-select v-model="q.status" placeholder="状态" clearable style="width: 140px; margin-left:12px;">
          <el-option label="待审核" value="PENDING" />
          <el-option label="已通过" value="APPROVED" />
          <el-option label="已驳回" value="REJECTED" />
        </el-select>
        <el-date-picker v-model="q.range" type="daterange" range-separator="至" start-placeholder="开始" end-placeholder="结束" value-format="YYYY-MM-DD" style="margin-left:12px;" />
        <el-button type="primary" :loading="loading" style="margin-left:12px;" @click="fetchAll">查询</el-button>
      </div>
    </div>

    <el-card>
      <el-table :data="rows" stripe v-loading="loading">
        <el-table-column prop="workType" label="类型" width="100" />
        <el-table-column prop="category" label="类别" width="200" />
        <el-table-column prop="content" label="内容" min-width="260" show-overflow-tooltip />
        <el-table-column prop="targetOrHours" label="目标/时长" width="140" />
        <el-table-column prop="status" label="状态" width="110">
          <template #default="{ row }"><el-tag :type="tag(row.status)">{{ text(row.status) }}</el-tag></template>
        </el-table-column>
        <el-table-column label="操作" width="240" fixed="right">
          <template #default="{ row }">
            <el-button type="text" @click="openReview(row)">审核</el-button>
            <el-button type="text" @click="edit(row)">编辑</el-button>
            <el-button type="text" style="color:#f56c6c;" @click="removeRow(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 编辑 -->
    <el-dialog v-model="showEdit" title="编辑服务贡献" width="640px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="类型"><el-select v-model="form.workType" style="width:100%" @change="onTypeChange"><el-option label="临时性" value="临时性"/><el-option label="长期性" value="长期性"/></el-select></el-form-item>
        <el-form-item label="工作类别"><el-select v-model="form.category" style="width:100%"><el-option v-for="opt in categoryOptions" :key="opt" :label="opt" :value="opt" /></el-select></el-form-item>
        <el-form-item label="具体内容"><el-input v-model="form.content" type="textarea" :rows="4"/></el-form-item>
        <el-form-item label="目标/时长" required><el-input v-model="form.targetOrHours" placeholder="例如 40小时/全年"/></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showEdit=false">取消</el-button>
        <el-button type="primary" :loading="saving" @click="submitEdit">保存</el-button>
      </template>
    </el-dialog>

    <!-- 审核 -->
    <el-dialog v-model="showReview" title="审核服务贡献" width="520px">
      <el-form :model="reviewForm" label-width="96px">
        <el-form-item label="审核结果" required>
          <el-radio-group v-model="reviewForm.status">
            <el-radio label="APPROVED">通过</el-radio>
            <el-radio label="REJECTED">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见">
          <el-input v-model="reviewForm.opinion" type="textarea" :rows="4" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showReview=false">取消</el-button>
        <el-button type="primary" :loading="reviewLoading" @click="submitReview">提交</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getAllServices, updateService, deleteService, reviewService } from '@/api/serviceContribution'

const q = reactive({ type: '', status: '', range: [] })
const rows = ref([])
const loading = ref(false)
const showEdit = ref(false)
const showReview = ref(false)
const saving = ref(false)
const reviewLoading = ref(false)
const currentId = ref(null)
const form = reactive({ id: undefined, workType: '临时性', category: '', content: '', targetOrHours: '' })
const categoryOptions = ref([])
const reviewForm = reactive({ status: '', opinion: '' })

function text(s){ return ({ PENDING:'待审核', APPROVED:'已通过', REJECTED:'已驳回' }[s] || '待审核') }
function tag(s){ return ({ PENDING:'warning', APPROVED:'success', REJECTED:'danger' }[s] || 'info') }

async function fetchAll(){
  loading.value = true
  try{
    const res = await getAllServices({ type: q.type || undefined, status: q.status || undefined, startDate: q.range?.[0], endDate: q.range?.[1] })
    rows.value = res?.data || res || []
  } finally { loading.value = false }
}

function edit(row){ Object.assign(form, row); showEdit.value = true }
async function submitEdit(){
  if(!form.category){ ElMessage.warning('请填写工作类别'); return }
  saving.value = true
  try{ await updateService(form.id, { work_type: form.workType, category: form.category, content: form.content, target_or_hours: form.targetOrHours }); ElMessage.success('已保存'); showEdit.value=false; fetchAll() } finally { saving.value = false }
}

async function removeRow(row){ await ElMessageBox.confirm('确定删除？','提示',{type:'warning'}); await deleteService(row.id); ElMessage.success('已删除'); fetchAll() }

function openReview(row){ currentId.value = row.id; reviewForm.status=''; reviewForm.opinion=''; showReview.value=true }
async function submitReview(){ if(!reviewForm.status){ ElMessage.warning('请选择结果'); return } reviewLoading.value=true; try{ await reviewService(currentId.value, reviewForm); ElMessage.success('审核完成'); showReview.value=false; fetchAll() } finally { reviewLoading.value=false } }

onMounted(fetchAll)

function onTypeChange(){
  categoryOptions.value = form.workType === '临时性'
    ? ['起草文稿', '参加学院/市教委创新大赛', '举办或参与学院/系重要活动', 'MPA教育中心工作', '转专业相关工作', '培养方案修订工作', '其他']
    : ['工会小组长', '大创管理工作', '信息员', 'MPA教育中心兼职秘书', '其他']
  form.category = ''
}
onTypeChange()
</script>

<style scoped>
.svc-overview { max-width: 1400px; }
.page-header { display:flex; justify-content:space-between; align-items:center; margin-bottom:16px; }
.filters { display:flex; align-items:center; }
</style>


